#include <QApplication>
#include <QMainWindow>
#include <QAction>
#include <QAction>
#include <QMenuBar>
#include <QWidget>
#include <QToolBar>
#include <QPlainTextEdit>
#include <QFileDialog>
#include <QDockWidget>
#include <QLabel>
#include <QStatusBar>
#include <QGridLayout>
#include <QPushButton>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QDesktopWidget>
#include <QCheckBox>
#include <QListWidget>
#include <QStackedWidget>
#include <QDir>
#include <QFont>
#include <QIcon>
#include <QStringList>
#include <QMessageBox>
#include <QDebug>
//for xml parse (must add "Qt += xml" to .pro file)
#include <QDomDocument>
#include <QDomElement>
#include <QDomNode>
#include <QDomNodeList>
//for stardard c++
#include <iostream>
using namespace std;

#include "mainwindow.h"
//#include "reciteword.h"
//#include "settingdialog.h"

void MainWindow::showSettingPage()
{
  if(sd==NULL)
    {
      sd = new SettingDialog(this);
      sd->setPaths(rw->getPaths());
      sw->addWidget(sd);
    }
  //sd->setPaths(rw->getPaths());		//actually not needed except the first time
  sw->setCurrentIndex(1);
}
void MainWindow::showRecitePage()
{
  //if paths not modified, do not initReciteWord again !
  if(sd != NULL)
    {
      //sd exist, must read paths from sd
      QStringList sl = sd->getPaths();
      if(sl == rw->getPaths())
	{
	  //paths not modified, not re-init
	  if(rw->initOK() == false)
	    {
	      //init failed && not modify paths, go to setting page
	      sw->setCurrentIndex(1);
	    }
	  else
	    sw->setCurrentIndex(0);	//back to reciteword, modify nothing
	}
      else
	{
	  //paths modified, re-init
	  if(rw->initOK())	
	    rw->record();			//record the data of previous dict
	  rw->setPaths(sl);	
	  if(rw->initReciteWord())	//init succeed
	    sw->setCurrentIndex(0);
	  else
	    sw->setCurrentIndex(1);
	}
    }
  else
    {
      //sd not exist(init succeed),just show it
      sw->setCurrentIndex(0);
    }
}

MainWindow::MainWindow(QWidget *parent)
  : QMainWindow(parent)
{

  //QFont userFont("Courier New", 18, QFont::bold);
  QFont userFont("Courier New", 18);
  //setStyleSheet("QToolBar{font-size:20pt;color:red}");	//not available

  //QActions
  QAction *settingAct= new QAction(tr("&Setting"), this);
  settingAct->setFont(userFont);
  //connect(settingAct, SIGNAL(triggered()), this, SLOT(setting()));
  connect(settingAct, SIGNAL(triggered()), this, SLOT(showSettingPage()));
  //newAct->setShortcuts(QKeySequence::New);
  //newAct->setStatusTip(tr("settings"));

  QAction *quitAct = new QAction(tr("&Quit"), this);
  quitAct->setFont(userFont);
  //quitAct->setShortcuts(QKeySequence::Quit);
  //quitAct->setStatusTip(tr("Quit"));
  connect(quitAct, SIGNAL(triggered()), this, SLOT(close()));
  /*  QAction *aboutAct = new QAction(tr("&About"), this);
  aboutAct->setStatusTip(tr("show the app's about box"));
  connect(aboutAct, SIGNAL(triggered()), this, SLOT(about()));
  */

  //disable or enable some actions
  //newAct->setEnabled(false);

  //create toolbar

  //QToolBar *fileToolBar = addToolBar(tr("ReciteControl"));	//the toolbar disappear while fullscreen when before  Qt 4.5
  QToolBar *fileToolBar = addToolBar(tr("ReciteControl"));	//the toolbar disappear while fullscreen when before  Qt 4.5
  fileToolBar->addAction(settingAct);
  fileToolBar->addSeparator();
  fileToolBar->addAction(quitAct);
  fileToolBar->setFloatable(false);
  fileToolBar->setAllowedAreas(Qt::TopToolBarArea);
  addToolBar(Qt::TopToolBarArea, fileToolBar);

  //QWidget *centralWidget = new QWidget(this);
  //setCentralWidget(centralWidget);
  //  textEdit = new QPlainTextEdit;
  //setCentralWidget(textEdit);

  //SettingDialog *sd = new SettingDialog(this);
  //ReciteWord *rw = new ReciteWord(this, sd);
  rw = new ReciteWord(this);
  sw = new QStackedWidget;
  sw->addWidget(rw);		//SettingDialog created in showSettingPage() when needed
  //sw->addWidget(sd);
  //sw->setCurrentIndex(0);
  setCentralWidget(sw);

  sd = NULL;
  if(rw->readConfigFile() && rw->initReciteWord())
    {
      showRecitePage();
    }
  else
    {
      //configfile not exist or read it failed 
      showSettingPage();	//contain : create SettingDiaglog
    }

  //  setWindowFlags(Qt::FramelessWindowHint);
  //statusBar()->showMessage(tr("ready"));
  //statusBar()->showMessage(tr("ready"), 2000);
}


void MainWindow::test()
{
  //showFullScreen();
  //fileToolBar->show();	//no usefull for Qt before 4.5
}

